package com.wu.lib.utls;

import android.content.Context;
import android.content.res.AssetManager;
import android.util.Log;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

public class PropertiesFile {
    private Map<String, String> mProperties = new HashMap<>();

    public PropertiesFile(Context context, String file) {
        Properties prop = new Properties();
        AssetManager am = context.getAssets();
        InputStream input = null;
        try {
            input = am.open(file);
            InputStream in = new BufferedInputStream(input);
            prop.load(in);
            Iterator<String> it = prop.stringPropertyNames().iterator();
            while (it.hasNext()) {
                String key = it.next();
                String value = prop.getProperty(key);
                mProperties.put(key, value);
                Log.d("DFresco", key + "=" + value);
            }
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String getValue(String key, String defaultValue) {
        String value = mProperties.get(key);
        if (value == null) {
            value = defaultValue;
        }
        return value;
    }
}
